<template>
  <el-dialog width="1000px" v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="所属接口" prop="invokeId">
        <!--        <el-input v-model="dataForm.invokeId" placeholder="所属接口"></el-input>-->
        <el-select v-model="dataForm.invokeId"  placeholder="所属接口">
          <el-option v-for="role in invokeList" :key="role.id" :label="role.invokeName" :value="role.id"></el-option>
        </el-select>
      </el-form-item>
     <el-form-item label="父ID" prop="pid">
        <el-input v-model="dataForm.pid" placeholder="父ID"></el-input>
      </el-form-item>

      <el-form-item label="目标-字段" prop="targetFields">
        <el-input v-model="dataForm.targetFields" placeholder="目标-字段"></el-input>
      </el-form-item>
      <el-form-item label="目标-类型" prop="targetType">
        <el-input v-model="dataForm.targetType" placeholder="目标-类型"></el-input>
      </el-form-item>
      <el-form-item label="目标-默认值" prop="targetDefault">
        <el-input v-model="dataForm.targetDefault" placeholder="目标-默认值"></el-input>
      </el-form-item>
      <el-form-item label="目标-说明" prop="targetComment">
        <el-input v-model="dataForm.targetComment" placeholder="目标-说明"></el-input>
      </el-form-item>
          <el-form-item label="传递-表" prop="transmitTable">
        <el-input v-model="dataForm.transmitTable" placeholder="传递-表"></el-input>
      </el-form-item>
          <el-form-item label="传递-表名" prop="transmitTableName">
        <el-input v-model="dataForm.transmitTableName" placeholder="传递-表名"></el-input>
      </el-form-item>
          <el-form-item label="传递-字段" prop="transmitFields">
        <el-input v-model="dataForm.transmitFields" placeholder="传递-字段"></el-input>
      </el-form-item>
          <el-form-item label="传递-字段名" prop="transmitFieldsName">
        <el-input v-model="dataForm.transmitFieldsName" placeholder="传递-字段名"></el-input>
      </el-form-item>
          <el-form-item label="传递-类型" prop="transmitType">
        <el-input v-model="dataForm.transmitType" placeholder="传递-类型"></el-input>
      </el-form-item>
          <el-form-item label="传递说明" prop="transmitComment">
        <el-input v-model="dataForm.transmitComment" placeholder="传递说明"></el-input>
      </el-form-item>
          <el-form-item label="传递类型" prop="replaceTypeGroovy">
        <el-input v-model="dataForm.replaceTypeGroovy" placeholder="传递类型"></el-input>
      </el-form-item>
          <el-form-item label="是否删除" prop="isDel">
        <el-input v-model="dataForm.isDel" placeholder="是否删除"></el-input>
      </el-form-item>
          </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const invokeList = ref<any[]>([]);

const dataForm = reactive({
  id: '',  pid: '',  invokeId: '',  targetFields: '',  targetType: '',  targetDefault: '',  targetComment: '',  transmitTable: '',  transmitTableName: '',  transmitFields: '',  transmitFieldsName: '',  transmitType: '',  transmitComment: '',  replaceTypeGroovy: '',  isDel: '',  createDate: '',  updateDate: ''});

const rules = ref({
    //       pid: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
          invokeId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
    //       targetFields: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       targetType: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       targetDefault: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       targetComment: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitTable: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitTableName: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitFields: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitFieldsName: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitType: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       transmitComment: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       replaceTypeGroovy: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
    //       isDel: [
    //   { required: true, message: '必填项不能为空', trigger: 'blur' }
    // ],
      });

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  getInvokeIdList();
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/oatools/fields/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/oatools/fields", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

// 获取系统信息
const getInvokeIdList = () => {
  baseService.get("/oatools/invoke/list").then((res) => {
    invokeList.value = res.data;

  });
};

defineExpose({
  init
});
</script>
